<template>
  <page-layout :noToolbar="true">

    <el-card class="box-card">
      <div class="card-header">个人完成率统计</div>
      <el-card class="box-card">
        <el-form ref="searchStatisticCondition" :model="searchStatisticCondition" label-width="80px" inline>
          <el-form-item label="企业名称" v-if="adminInfo.username==='admin'">
            <el-autocomplete
              v-model="searchStatisticCondition.departName"
              :fetch-suggestions="querySearchAsync"
              placeholder="请输入内容"
              @select="handleSelect"
            ></el-autocomplete>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="searchStatisticCondition.name" />
          </el-form-item>
          <el-form-item label="排序">
            <el-select v-model="searchStatisticCondition.by" placeholder="请选择" >
              <el-option
                v-for="item in SortCategory"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序方式">
            <el-select v-model="searchStatisticCondition.order" placeholder="请选择" >
              <el-option
                v-for="item in SortType"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="查询时间">
            <el-date-picker
              v-model="value4"
              type="daterange"
              range-separator="至"
              start-placeholder="查询开始日期"
              end-placeholder="查询截至日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refresh(1)">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
          <el-card class="box-card">
            <el-table
              :data="statisticList.content" border stripe
              v-loading="loading"
              element-loading-text="拼命统计中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(0, 0, 0, 0.8)">
              <el-table-column
                prop="id"
                label="编号"
                width="100">
              </el-table-column>
              <el-table-column prop="name" label="姓名">
              </el-table-column>
              <el-table-column prop="departName" label="所属单位（企业）"  v-if="adminInfo.userRole!=='COM'">
              </el-table-column>
              <el-table-column prop="trainDoneRate" label="每日练习完成率" >
                <template slot-scope="scope">
                  {{scope.row.trainDoneRate}}
                </template>
              </el-table-column>
              <el-table-column
                prop="taskDoneRate"
                label="下发任务完成率">
                <template slot-scope="scope">
                  {{scope.row.taskDoneRate}}
                </template>
              </el-table-column>
              <el-table-column
                prop="questCorrectRate"
                label="答题正确率">
                <template slot-scope="scope">
                  {{scope.row.questCorrectRate}}
                </template>
              </el-table-column>
              <el-table-column
                prop="newsCount"
                label="咨询浏览总数">
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="statisticList.totalElements"
                :page-size="statisticList.size"
                :current-page.sync="currentStatisticPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
    </el-card>
  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import { mapGetters } from "vuex";
import Status from "@/libs/status";
import Moment from "moment";
export default {
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.departId = to.query.departId;
      vm.searchStatisticCondition.departName = to.query.departName;
      // vm.refresh();
    });
  },
  created() {},
  mounted() {},
  data() {
    return {
      departId: "",
      loading: false,
      currentStatisticPage: 1,
      showEditor: false,
      StateTypes: Status.StateTypes,
      SortType: Status.SortType,
      SortCategory: Status.SortCategory,
      addForm: { content: "" },
      value4: [],
      departList: [],
      timeout: null
    };
  },
  computed: {
    ...mapGetters({
      statisticList: "statisticList",
      currentStatistic: "currentStatistic",
      adminInfo: "adminInfo",
      searchStatisticCondition: "searchStatisticCondition"
    })
  },
  methods: {
    querySearchAsync(queryString, cb) {
      var restaurants = this.departList;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;

      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 3000 * Math.random());
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },
    loadAll() {
      this.$store.dispatch("getDepartListS").then(ret => {
        var tem = ret.data.data;
        if (tem) {
          tem.map((item, index) => {
            this.departList.push({ value: item.name, id: item.id });
          });
        }
      });
    },
    handleSelect(item) {
      console.log(item);
      this.searchStatisticCondition.departName = item.value;
    },
    getPageChange(val) {
      this.searchStatisticCondition.startDate = "";
      this.searchStatisticCondition.endDate = "";
      this.loading = true;
      if (this.value4 && this.value4.length > 0) {
        this.searchStatisticCondition.startDate = Moment(this.value4[0]).format(
          "YYYY-MM-DD"
        );
        this.searchStatisticCondition.endDate = Moment(this.value4[1]).format(
          "YYYY-MM-DD"
        );
      }
      var tem = Object.assign(
        { pageIndex: val - 1 },
        this.searchStatisticCondition
      );
      this.$store.dispatch("getPersonalStatisticPageS", tem).then(ret => {
        this.loading = false;
      });
    },
    refresh() {
      this.getPageChange(1);
      this.loadAll();
    },
    back() {
      this.showEditor = false;
      this.refresh();
    },
    save() {
      var tem = this.currentStatistic;
      tem.status = 1;
      tem.remark = this.addForm.content;
      this.$store.dispatch("addOrUpdateStatisticS", tem).then(ret => {
        this.refresh();
      });
    },
    editStatistic(row) {
      this.showEditor = true;
      this.$store.dispatch("getStatisticS", row.id).then(ret => {});
    },
    deleteStatistic(row) {
      this.$confirm("确认删除当前意见反馈吗?", "提示", {})
        .then(() => {
          this.$store.dispatch("deleteStatisticS", { id: row.id }).then(ret => {
            this.refresh();
            this.$notify({
              title: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    }
  },
  components: {
    PageLayout
  }
};
</script>
<style scoped>
</style>
